using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Markup;
using Common;
using Common.Enum;
using Controls;
using Microsoft.Win32;
using PCR.PCRControls;
using SProject;
using SProject.Paramter;
using SProject.Program;

namespace ProjectView.Windows
{
	public partial class ProjectModifyMainWindow : UserControl, IComponentConnector
	{
		public delegate void DelegateOnProjectEvent(object sender, EventArgs e);

		public delegate bool DelegateOnProjectContainsEvent(object sender, EventArgs e);

		public delegate List<Project> DelegateOnGetProjectListEvent(object sender, EventArgs e);

		public delegate string DelegateOnGetRelativePathEvent(object sender, EventArgs e);

		public delegate void DelegateOnProjectCallbackEvent(object sender, string actionName, Action callback);

		private bool _IsRestore = true;

		private bool _IsModify;

		private Project _ModifyProject;

		private Project _Project;

		private string _password;

		private RadioButton _ErrorRadioButton;

		public Project ModifyProject
		{
			get
			{
				return _ModifyProject;
			}
		}

		public Project Project
		{
			get
			{
				return _Project;
			}
		}

		public ObservableCollection<string> ColusionDisplayList { get; private set; }

		public List<KeyValuePair> ProjectTypeList
		{
			get
			{
				return EnumManagement.GetAllProjectType();
			}
		}

		public event DelegateOnProjectEvent OnProjectSave;

		public event DelegateOnProjectEvent OnProjectClose;

		public event DelegateOnProjectContainsEvent OnProjectContains;

		public event DelegateOnProjectContainsEvent OnIsExistProjectName;

		public event DelegateOnProjectContainsEvent OnProjectCanAdd;

		public event DelegateOnGetProjectListEvent OnGetAllProjectList;

		public event DelegateOnGetRelativePathEvent OnGetRelativePath;

		public event DelegateOnProjectCallbackEvent OnProjectSaveCallback;

		public ProjectModifyMainWindow(Project prj)
		{
			InitializeComponent();
			_Project = prj;
			Init();
		}

		public bool Save()
		{
			return SaveProject();
		}

		public bool Close(bool save = true)
		{
			if (save && IsModify())
			{
				switch (MessageBox.Show(string.Format(Utility.GetMessage("msg_save_project_confirm"), _Project.BasicInfo.Name), Utility.GetMessage("Warning"), MessageBoxButton.YesNoCancel))
				{
				case MessageBoxResult.Cancel:
					return false;
				case MessageBoxResult.Yes:
					CommandBinding_Save_Executed(null, null);
					if (_IsModify)
					{
						return false;
					}
					break;
				}
			}
			if (this.OnProjectClose != null)
			{
				this.OnProjectClose(this, new EventArgs());
			}
			return true;
		}

		private void Init()
		{
			if (_Project == null)
			{
				_Project = new Project();
			}
			ColusionDisplayList = new ObservableCollection<string>();
			_ModifyProject = new Project(_Project);
			_IsRestore = _ModifyProject.Target.Items.Count == 0;
			base.DataContext = _ModifyProject;
			cmbType.SelectedValue = _ModifyProject.BasicInfo.Type;
			RefreshTarget();
			InitPCRProgramMenu();
			if ((_ModifyProject.BasicInfo.Type == EProjectType.AQ || _ModifyProject.BasicInfo.Type == EProjectType.Melt) && ConfigReader.GetInstance().GetMutilpleTarget())
			{
				PanelMutipleTarget.Visibility = Visibility.Visible;
			}
			else
			{
				PanelMutipleTarget.Visibility = Visibility.Collapsed;
			}
			numTubeNum.ValueChanged += numTubeNum_ValueChanged;
			numTargetNum.ValueChanged += numTargetNum_ValueChanged;
			base.SourceUpdated += Root_SourceUpdated;
		}

		private void InitPCRProgramMenu()
		{
			object obj = base.Resources["menuProgram"];
			if (obj != null)
			{
				UCPCRProgram.AddCustomMenu(obj as ContextMenu);
			}
		}

		private bool SaveProject()
		{
			if (!Validate())
			{
				return false;
			}
			if (this.OnGetRelativePath != null)
			{
				_ModifyProject.BasicInfo.RelativePath = this.OnGetRelativePath(this, new EventArgs());
			}
			_Project.DeleteFile();
			_ModifyProject.Target.UpdateChannels();
			_ModifyProject.Save();
			_Project.Copy(_ModifyProject);
			return true;
		}

		private bool Validate()
		{
			if (!ValidateBasicInfo() || !ValidateTarget())
			{
				((RadioButton)spRadio.Children[0]).IsChecked = true;
				return false;
			}
			if (!ValidateProgram())
			{
				((RadioButton)spRadio.Children[1]).IsChecked = true;
				return false;
			}
			if (!ValidateParamter())
			{
				((RadioButton)spRadio.Children[2]).IsChecked = true;
				return false;
			}
			if (!ValidateRule())
			{
				((RadioButton)spRadio.Children[3]).IsChecked = true;
				return false;
			}
			return true;
		}

		private bool ValidateBasicInfo(bool showMsg = true)
		{
			if (string.IsNullOrEmpty(_ModifyProject.BasicInfo.Name) || string.IsNullOrEmpty(_ModifyProject.BasicInfo.Name.Trim()))
			{
				MessageBox.Show(string.Format(Utility.GetMessage("prj_msg_filename_is_null")));
				teName.Focus();
				return false;
			}
			if (!Utility.ValidateFileName(_ModifyProject.BasicInfo.Name))
			{
				MessageBox.Show(string.Format(Utility.GetMessage("msg_filename_illegalcharacter"), Utility.GetFileNameIllegalCharacter().ToString()));
				teName.Focus();
				return false;
			}
			if (_ModifyProject.BasicInfo.Reaction < 15.0 || _ModifyProject.BasicInfo.Reaction > 100.0)
			{
				MessageBox.Show(string.Format(Utility.GetMessage("msg_reaction_over_range"), 15, 100));
				teReaction.Focus();
				return false;
			}
			bool flag = false;
			bool flag2 = false;
			if ((this.OnIsExistProjectName == null) ? ProjectManage.GetInstance().IsExistProjectName(_ModifyProject.BasicInfo.Name) : this.OnIsExistProjectName(this, new EventArgs()))
			{
				bool flag3 = false;
				if ((this.OnProjectContains == null) ? ProjectManage.GetInstance().ContainsProject(_Project) : this.OnProjectContains(this, new EventArgs()))
				{
					if (_Project.BasicInfo.Name != _ModifyProject.BasicInfo.Name)
					{
						flag = true;
					}
				}
				else
				{
					flag = true;
				}
			}
			if (flag)
			{
				MessageBox.Show(string.Format(Utility.GetMessage("msg_project_name_exist"), _ModifyProject.BasicInfo.Name));
				teName.Focus();
				return false;
			}
			return true;
		}

		private bool ValidateProgram(bool showMsg = true)
		{
			_ModifyProject.PCRProgram = UCPCRProgram.GetData();
			List<Segment> list = _ModifyProject.PCRProgram.Programs.Where((BaseSegment s) => s is Segment).Cast<Segment>().ToList();
			int num = 0;
			foreach (Segment item in list)
			{
				num += item.Items.Where((SProject.Program.Step s) => s.IsAcquisition).Count();
			}
			if ((double)num != numTargetNum.Value && numTargetNum.Value > 1.0)
			{
				MessageBox.Show("采集荧光数与单个通道的目标数不符", Utility.GetMessage("Warning"));
				return false;
			}
			if (double.IsNaN(_ModifyProject.PCRProgram.GetMeltingMaxTemp()) && _ModifyProject.BasicInfo.Type != EProjectType.NFS)
			{
				int acquisitionCycleCount = _ModifyProject.PCRProgram.GetAcquisitionCycleCount();
				if (acquisitionCycleCount == 0)
				{
					if (MessageBox.Show(Utility.GetMessage("msg_no_acquisition_save"), Utility.GetMessage("Warning"), MessageBoxButton.YesNo) == MessageBoxResult.No)
					{
						return false;
					}
				}
				else if (acquisitionCycleCount < 3)
				{
					MessageBox.Show(string.Format(Utility.GetMessage("msg_project_acquisition_cycle_not_enough"), 3));
					return false;
				}
				foreach (Segment item2 in list)
				{
					if (item2.Items.Where((SProject.Program.Step s) => s.IsAcquisition).Count() > 0 && item2.Cycles < 3 && MessageBox.Show(string.Format(Utility.GetMessage("msg_acquisition_cycle_not_enough"), item2.Name, 3), Utility.GetMessage("Warning"), MessageBoxButton.YesNo) == MessageBoxResult.No)
					{
						return false;
					}
				}
			}
			return true;
		}

		private bool ValidateParamter(bool showMsg = true)
		{
			bool flag = false;
			if (!(flag = uCProjectBasicOption.Validate(_ModifyProject.PCRProgram.GetAcquisitionCycleCount(), showMsg)))
			{
				((TabItem)dXTabControlParamter.Items[0]).IsSelected = true;
				return flag;
			}
			if (ModifyProject.BasicInfo.Type == EProjectType.Melt)
			{
				if (!(flag = uCProjectMeltBasicOption.Validate()))
				{
					((TabItem)dXTabControlParamter.Items[1]).IsSelected = true;
					return flag;
				}
				return true;
			}
			if (ModifyProject.BasicInfo.Type == EProjectType.HRM && !(flag = uCHRMOption.Validate()))
			{
				((TabItem)dXTabControlParamter.Items[2]).IsSelected = true;
				return flag;
			}
			if (ModifyProject.BasicInfo.Type == EProjectType.RQ && !(flag = ucRQOption.Validate()))
			{
				tabItemRQOption.IsSelected = true;
				return flag;
			}
			if (uCProjectDetection.Validate(showMsg))
			{
				return uCProjectCrossTalk.Validate(showMsg);
			}
			return false;
		}

		private bool ValidateTarget(bool showMsg = true)
		{
			if (!uCProjectTarget.SaveTarget(showMsg))
			{
				return false;
			}
			_ModifyProject.UpdataTarget(uCProjectTarget.Target);
			uCProjectCrossTalk.RefreshCrossTalk();
			return true;
		}

		private bool ValidateRule(bool showMsg = true)
		{
			return ucRule.Validate();
		}

		private DataGridEx GetSelectedGridControl()
		{
			DataGridEx result = null;
			if (TabControl.SelectedItem == dXTabItemParamter)
			{
				if (uCProjectBasicOption.GridControl.SelectedCells.Count > 0)
				{
					result = uCProjectBasicOption.GridControl;
				}
				else if (uCProjectMeltBasicOption.grid.SelectedCells.Count > 0)
				{
					result = uCProjectMeltBasicOption.grid;
				}
				else if (uCHRMOption.grid.SelectedCells.Count > 0)
				{
					result = uCHRMOption.grid;
				}
				else if (uCADOption.grid.SelectedCells.Count > 0)
				{
					result = uCADOption.grid;
				}
				else if (ucRQOption.grid.SelectedCells.Count > 0)
				{
					result = ucRQOption.grid;
				}
				else if (uCProjectDetection.GridControl.SelectedCells.Count > 0)
				{
					result = uCProjectDetection.GridControl;
				}
			}
			return result;
		}

		private void RefreshMeltParamterVisible()
		{
			_ModifyProject.PCRProgram = UCPCRProgram.GetData();
			tabItemMeltParamter.Visibility = ((_ModifyProject.BasicInfo.Type != EProjectType.Melt && (_ModifyProject.BasicInfo.Type != EProjectType.IA || _ModifyProject.PCRProgram.Programs.Where((BaseSegment s) => s is MeltingSegment).Count() <= 0)) ? Visibility.Collapsed : Visibility.Visible);
		}

		private void numTubeNum_ValueChanged(object sender, DependencyPropertyChangedEventArgs args)
		{
			if (args.OldValue != null && args.NewValue != null && !(args.OldValue.ToString() == args.NewValue.ToString()))
			{
				if (int.Parse(args.NewValue.ToString()) < int.Parse(args.OldValue.ToString()) && MessageBox.Show(Utility.GetMessage("Msg_TubeCount_Minus_Confirm"), Utility.GetMessage("Warning"), MessageBoxButton.YesNo, MessageBoxImage.Exclamation) == MessageBoxResult.No)
				{
					numTubeNum.Value = int.Parse(args.OldValue.ToString());
					return;
				}
				uCProjectTarget.SetTubeCount((int)numTubeNum.Value);
				radBtnTubeSortByColumn.Visibility = (((int)numTubeNum.Value == 1) ? Visibility.Collapsed : Visibility.Visible);
				radBtnTubeSortByRow.Visibility = radBtnTubeSortByColumn.Visibility;
				tubeOrder.Visibility = radBtnTubeSortByColumn.Visibility;
				SetModify();
			}
		}

		private void numTargetNum_ValueChanged(object sender, DependencyPropertyChangedEventArgs args)
		{
			if (args.OldValue != null && args.NewValue != null && !(args.OldValue.ToString() == args.NewValue.ToString()))
			{
				if (int.Parse(args.NewValue.ToString()) < int.Parse(args.OldValue.ToString()) && MessageBox.Show(Utility.GetMessage("Msg_TubeCount_Minus_Confirm"), Utility.GetMessage("Warning"), MessageBoxButton.YesNo, MessageBoxImage.Exclamation) == MessageBoxResult.No)
				{
					numTargetNum.Value = int.Parse(args.OldValue.ToString());
					return;
				}
				uCProjectTarget.SetTargetCount((int)numTargetNum.Value);
				SetModify();
			}
		}

		private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{
			if (e.OriginalSource == TabControl)
			{
				menuProgramEdit.Visibility = ((TabControl.SelectedItem != dXTabItemProgram) ? Visibility.Collapsed : Visibility.Visible);
				menuParameterEdit.Visibility = ((TabControl.SelectedItem != dXTabItemParamter) ? Visibility.Collapsed : Visibility.Visible);
				if (TabControl.SelectedItem == dXTabItemParamter)
				{
					RefreshMeltParamterVisible();
					dXTabControlParamter.RefreshTabItemStyle();
				}
			}
		}

		private void uCProjectBasicOption_GotFocus(object sender, RoutedEventArgs e)
		{
			uCProjectMeltBasicOption.UnSelectAll();
			uCHRMOption.UnSelectAll();
			uCADOption.UnSelectAll();
			ucRQOption.UnSelectAll();
			uCProjectDetection.UnSelectAll();
			uCProjectCrossTalk.UnSelectAll();
		}

		private void uCProjectDetection_GotFocus(object sender, RoutedEventArgs e)
		{
			uCProjectCrossTalk.UnSelectAll();
			uCProjectBasicOption.UnSelectAll();
			uCProjectMeltBasicOption.UnSelectAll();
			uCHRMOption.UnSelectAll();
			uCADOption.UnSelectAll();
			ucRQOption.UnSelectAll();
		}

		private void uCProjectCrossTalk_GotFocus(object sender, RoutedEventArgs e)
		{
			uCProjectBasicOption.UnSelectAll();
			uCProjectDetection.UnSelectAll();
			uCProjectMeltBasicOption.UnSelectAll();
			uCHRMOption.UnSelectAll();
			uCADOption.UnSelectAll();
			ucRQOption.UnSelectAll();
		}

		private void uCProjectMeltBasicOption_GotFocus(object sender, RoutedEventArgs e)
		{
			uCProjectBasicOption.UnSelectAll();
			uCHRMOption.UnSelectAll();
			uCADOption.UnSelectAll();
			ucRQOption.UnSelectAll();
			uCProjectDetection.UnSelectAll();
			uCProjectCrossTalk.UnSelectAll();
		}

		private void UCHRMOption_GotFocus(object sender, RoutedEventArgs e)
		{
			uCProjectBasicOption.UnSelectAll();
			uCProjectMeltBasicOption.UnSelectAll();
			uCADOption.UnSelectAll();
			ucRQOption.UnSelectAll();
			uCProjectDetection.UnSelectAll();
			uCProjectCrossTalk.UnSelectAll();
		}

		private void UCADOption_GotFocus(object sender, RoutedEventArgs e)
		{
			uCProjectBasicOption.UnSelectAll();
			uCProjectMeltBasicOption.UnSelectAll();
			uCHRMOption.UnSelectAll();
			uCProjectDetection.UnSelectAll();
			uCProjectCrossTalk.UnSelectAll();
			ucRQOption.UnSelectAll();
		}

		private void UCRQOption_GotFocus(object sender, RoutedEventArgs e)
		{
			uCProjectBasicOption.UnSelectAll();
			uCProjectMeltBasicOption.UnSelectAll();
			uCHRMOption.UnSelectAll();
			uCProjectDetection.UnSelectAll();
			uCProjectCrossTalk.UnSelectAll();
			uCADOption.UnSelectAll();
		}

		private void CommandBinding_Save_CanExecute(object sender, CanExecuteRoutedEventArgs e)
		{
			e.CanExecute = true;
		}

		private void CommandBinding_Save_Executed(object sender, ExecutedRoutedEventArgs e)
		{
			if (this.OnProjectSaveCallback != null)
			{
				this.OnProjectSaveCallback(sender, Utility.GetMessage("Save"), delegate
				{
					SaveProject_Executed(sender, e);
				});
			}
			else
			{
				SaveProject_Executed(sender, e);
			}
		}

		private void SaveProject_Executed(object sender, ExecutedRoutedEventArgs e)
		{
			if (!SaveProject())
			{
				return;
			}
			bool flag = false;
			if (!((this.OnProjectContains == null) ? ProjectManage.GetInstance().ContainsProject(_Project) : this.OnProjectContains(this, new EventArgs())))
			{
				bool flag2 = true;
				if (this.OnProjectCanAdd != null)
				{
					flag2 = this.OnProjectCanAdd(this, new EventArgs());
				}
				if (flag2)
				{
					ProjectManage.GetInstance().AddProject(_Project);
				}
			}
			if (this.OnProjectSave != null)
			{
				this.OnProjectSave(this, new EventArgs());
			}
			SetModify(false);
		}

		private void barButtonItemClose_ItemClick(object sender, RoutedEventArgs e)
		{
			Close();
		}

		private void barButtonItemExport_ItemClick(object sender, RoutedEventArgs e)
		{
			if (!Validate())
			{
				return;
			}
			SaveFileDialog saveFileDialog = new SaveFileDialog();
			saveFileDialog.Filter = string.Format("{0}{1}|*.{2}", Project.ProjectFileExt, Utility.GetMessage("File"), Project.ProjectFileExt);
			saveFileDialog.FileName = _ModifyProject.BasicInfo.Name;
			saveFileDialog.FilterIndex = 1;
			saveFileDialog.DefaultExt = Project.ProjectFileExt;
			if (saveFileDialog.ShowDialog() == true)
			{
				if (_ModifyProject.SaveAs(saveFileDialog.FileName, true))
				{
					MessageBox.Show(Utility.GetMessage("msg_project_export_sussess"));
				}
				else
				{
					MessageBox.Show(Utility.GetMessage("msg_project_export_fail"));
				}
			}
		}

		private void barButtonItemPrint_ItemClick(object sender, RoutedEventArgs e)
		{
		}

		private void barButtonItemEncypt_ItemClick(object sender, RoutedEventArgs e)
		{
			if (!Validate())
			{
				return;
			}
			if (!string.IsNullOrEmpty(_ModifyProject.Password) && (string.IsNullOrEmpty(_password) || (!string.IsNullOrEmpty(_password) && !_ModifyProject.Password.Equals(_password))))
			{
				PasswordValidateWindow passwordValidateWindow = new PasswordValidateWindow();
				passwordValidateWindow.Password = _ModifyProject.Password;
				passwordValidateWindow.Owner = Utility.GetMainWindow();
				if (passwordValidateWindow.ShowDialog() != true)
				{
					return;
				}
				_password = passwordValidateWindow.InputPassword;
			}
			if (new ProjectEncryptWindow
			{
				Owner = Utility.GetMainWindow(),
				Project = _ModifyProject
			}.ShowDialog() == true)
			{
				GetSelectedGridControl();
			}
		}

		private void CommandBinding_DataCopy_CanExecute(object sender, CanExecuteRoutedEventArgs e)
		{
			DataGridEx selectedGridControl = GetSelectedGridControl();
			if (selectedGridControl != null)
			{
				if (selectedGridControl == uCProjectDetection.GridControl)
				{
					e.CanExecute = !ModifyProject.BasicOption.IsDetectionReadOnly && selectedGridControl.CommandCopyCellCanExecute();
				}
				else
				{
					e.CanExecute = !ModifyProject.BasicOption.IsReadOnly && selectedGridControl.CommandCopyCellCanExecute();
				}
			}
		}

		private void CommandBinding_DataCopy_Executed(object sender, ExecutedRoutedEventArgs e)
		{
			DataGridEx selectedGridControl = GetSelectedGridControl();
			if (selectedGridControl != null)
			{
				selectedGridControl.CommandCopyCellExecute();
				SetModify();
			}
		}

		private void CommandBinding_DataPaste_CanExecute(object sender, CanExecuteRoutedEventArgs e)
		{
			DataGridEx selectedGridControl = GetSelectedGridControl();
			if (selectedGridControl != null)
			{
				if (selectedGridControl == uCProjectDetection.GridControl)
				{
					e.CanExecute = !ModifyProject.BasicOption.IsDetectionReadOnly && selectedGridControl.CommandPasteCellCanExecute();
				}
				else
				{
					e.CanExecute = !ModifyProject.BasicOption.IsReadOnly && selectedGridControl.CommandPasteCellCanExecute();
				}
			}
		}

		private void CommandBinding_DataPaste_Executed(object sender, ExecutedRoutedEventArgs e)
		{
			DataGridEx selectedGridControl = GetSelectedGridControl();
			if (selectedGridControl != null)
			{
				selectedGridControl.CommandPasteCellExecute();
				SetModify();
			}
		}

		private void CommandBinding_ParamterDefault_CanExecute(object sender, CanExecuteRoutedEventArgs e)
		{
			e.CanExecute = !ModifyProject.BasicOption.IsReadOnly;
		}

		private void CommandBinding_ParamterDefault_Executed(object sender, ExecutedRoutedEventArgs e)
		{
			if (MessageBox.Show(Utility.GetMessage("msg_restore_default_question"), Utility.GetMessage("Warning"), MessageBoxButton.YesNo, MessageBoxImage.Exclamation, MessageBoxResult.No) != MessageBoxResult.Yes)
			{
				return;
			}
			foreach (BasicOptionItem item in _ModifyProject.BasicOption.Items)
			{
				item.SetDefaultData();
			}
			SetModify();
		}

		private void CommandBinding_ProgramAddMeltSection_CanExecute(object sender, CanExecuteRoutedEventArgs e)
		{
			e.CanExecute = !_ModifyProject.PCRProgram.IsReadOnly && UCPCRProgram.CanAddMelting();
		}

		private void CommandBinding_ProgramAddMeltSection_Executed(object sender, ExecutedRoutedEventArgs e)
		{
			UCPCRProgram.AddLast(ESectionType.Fution);
		}

		private void CommandBinding_ProgramAddSection_CanExecute(object sender, CanExecuteRoutedEventArgs e)
		{
			if (e.Parameter != null)
			{
				if (e.Parameter.Equals("Last"))
				{
					e.CanExecute = !_ModifyProject.PCRProgram.IsReadOnly;
				}
				else
				{
					e.CanExecute = !_ModifyProject.PCRProgram.IsReadOnly && UCPCRProgram.CanAddSection(e.Parameter.Equals("Before"));
				}
			}
		}

		private void CommandBinding_ProgramAddSection_Executed(object sender, ExecutedRoutedEventArgs e)
		{
			if (e.Parameter != null)
			{
				if (e.Parameter.Equals("After"))
				{
					UCPCRProgram.Add(ESectionType.Section, true);
				}
				else if (e.Parameter.Equals("Before"))
				{
					UCPCRProgram.Add();
				}
				else if (e.Parameter.Equals("Last"))
				{
					UCPCRProgram.AddLast();
				}
				SetModify();
			}
		}

		private void CommandBinding_ProgramAddStep_CanExecute(object sender, CanExecuteRoutedEventArgs e)
		{
			e.CanExecute = !_ModifyProject.PCRProgram.IsReadOnly && UCPCRProgram.CanAddStep();
		}

		private void CommandBinding_ProgramAddStep_Executed(object sender, ExecutedRoutedEventArgs e)
		{
			if (e.Parameter == null)
			{
				return;
			}
			if (e.Parameter.Equals("After"))
			{
				UCPCRProgram.Add(ESectionType.Step, true);
			}
			else if (e.Parameter.Equals("Before"))
			{
				UCPCRProgram.Add(ESectionType.Step);
			}
			else if (e.Parameter.Equals("Last"))
			{
				UCPCRProgram.AddLast(ESectionType.Step);
			}
			else if (e.Parameter.Equals("AdvPro"))
			{
				ISection section = UCPCRProgram.Items.Where((ISection s) => s.IsSelected).FirstOrDefault();
				if (section != null && section is Section && !(section as Section).EditProgram())
				{
					return;
				}
			}
			SetModify();
		}

		private void CommandBinding_ProgramItemDelete_CanExecute(object sender, CanExecuteRoutedEventArgs e)
		{
			e.CanExecute = !_ModifyProject.PCRProgram.IsReadOnly && (UCPCRProgram.CanDelete() || (_ModifyProject.BasicInfo.Type == EProjectType.IA && UCPCRProgram.GetSelected() is Fution));
		}

		private void CommandBinding_ProgramItemDelete_Executed(object sender, ExecutedRoutedEventArgs e)
		{
			UCPCRProgram.Remove();
			SetModify();
		}

		private void CommandBinding_UseProgram_CanExecute(object sender, CanExecuteRoutedEventArgs e)
		{
			List<Project> list = null;
			list = ((this.OnGetAllProjectList == null) ? ProjectManage.GetInstance().AllProjectList.ToList() : this.OnGetAllProjectList(this, new EventArgs()));
			List<Project> source = list.Where((Project s) => s != Project && s.BasicInfo.Type == _ModifyProject.BasicInfo.Type).ToList();
			e.CanExecute = !_ModifyProject.PCRProgram.IsReadOnly && source.Count() > 0;
		}

		private void CommandBinding_UseProgram_Executed(object sender, ExecutedRoutedEventArgs e)
		{
			List<Project> list = null;
			list = ((this.OnGetAllProjectList == null) ? ProjectManage.GetInstance().AllProjectList.ToList() : this.OnGetAllProjectList(this, new EventArgs()));
			List<Project> list2 = list.Where((Project s) => s != Project && s.BasicInfo.Type == _ModifyProject.BasicInfo.Type).ToList();
			UseProgramWindow useProgramWindow = new UseProgramWindow();
			useProgramWindow.Owner = Utility.GetMainWindow();
			useProgramWindow.SetList(list2);
			if (useProgramWindow.ShowDialog() == true)
			{
				SProject.Program.PCRProgram program = useProgramWindow.GetProgram();
				if (program != null)
				{
					UCPCRProgram.Program = program;
					SetModify();
				}
			}
		}

		private void barButtonItemHelp_ItemClick(object sender, RoutedEventArgs e)
		{
		}

		private void btnControl_Checked(object sender, RoutedEventArgs e)
		{
			if (_ErrorRadioButton != null)
			{
				_ErrorRadioButton.IsChecked = true;
				return;
			}
			RadioButton radioButton = sender as RadioButton;
			if (radioButton != null && radioButton.IsLoaded)
			{
				TabControl.SelectedIndex = Convert.ToInt32(radioButton.Tag);
			}
		}

		private void btnControl_Unchecked(object sender, RoutedEventArgs e)
		{
			if (_ErrorRadioButton != null)
			{
				_ErrorRadioButton = null;
				return;
			}
			RadioButton radioButton = sender as RadioButton;
			if (radioButton != null && radioButton.IsLoaded && radioButton.Tag != null && radioButton.Tag.ToString() == "0")
			{
				if (!ValidateBasicInfo() || !ValidateTarget())
				{
					_ErrorRadioButton = radioButton;
					e.Handled = true;
				}
				else
				{
					_ErrorRadioButton = null;
				}
			}
		}

		private void BasicInfo_SourceUpdated(object sender, DataTransferEventArgs e)
		{
			SetModify();
			e.Handled = true;
		}

		private void Root_SourceUpdated(object sender, DataTransferEventArgs e)
		{
			_IsRestore = false;
			SetModify();
		}

		private void SetModify(bool modify = true)
		{
			_IsModify = modify;
		}

		public bool IsModify()
		{
			return _IsModify;
		}

		private void cmbType_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{
			if (cmbType.SelectedValue == null)
			{
				return;
			}
			if (!_IsRestore && e.RemovedItems.Count > 0 && MessageBox.Show(Utility.GetMessage("msg_project_type_change"), Utility.GetMessage("Warning"), MessageBoxButton.OKCancel, MessageBoxImage.Exclamation, MessageBoxResult.Cancel) == MessageBoxResult.Cancel)
			{
				cmbType.SelectionChanged -= cmbType_SelectionChanged;
				cmbType.SelectedValue = _ModifyProject.BasicInfo.Type;
				cmbType.SelectionChanged += cmbType_SelectionChanged;
				return;
			}
			_IsRestore = true;
			_ModifyProject.BasicInfo.Type = (EProjectType)cmbType.SelectedValue;
			if ((_ModifyProject.BasicInfo.Type == EProjectType.AQ || _ModifyProject.BasicInfo.Type == EProjectType.Melt) && ConfigReader.GetInstance().GetMutilpleTarget())
			{
				PanelMutipleTarget.Visibility = Visibility.Visible;
			}
			else
			{
				PanelMutipleTarget.Visibility = Visibility.Collapsed;
			}
			if (numTubeNum.Value != 1.0)
			{
				numTubeNum.ValueChanged -= numTubeNum_ValueChanged;
				numTubeNum.Value = 1.0;
				numTubeNum.ValueChanged += numTubeNum_ValueChanged;
			}
			if (numTargetNum.Visibility == Visibility.Visible && numTargetNum.Value != 1.0)
			{
				numTargetNum.ValueChanged -= numTargetNum_ValueChanged;
				numTargetNum.Value = 1.0;
				numTargetNum.ValueChanged += numTargetNum_ValueChanged;
			}
			SetModify();
		}

		private void RefreshTarget()
		{
			if (_ModifyProject.Target.GetTubeCount() == 0)
			{
				numTubeNum.Value = 1.0;
			}
			else
			{
				numTubeNum.Value = _ModifyProject.Target.GetTubeCount();
			}
			if (_ModifyProject.Target.GetTargetCount() == 0)
			{
				numTargetNum.Value = 1.0;
			}
			else
			{
				numTargetNum.Value = _ModifyProject.Target.GetTargetCount();
			}
			radBtnTubeSortByColumn.Visibility = (((int)numTubeNum.Value == 1) ? Visibility.Collapsed : Visibility.Visible);
			radBtnTubeSortByRow.Visibility = radBtnTubeSortByColumn.Visibility;
			tubeOrder.Visibility = radBtnTubeSortByColumn.Visibility;
		}

		private void Command_ProgramT_Executed(object sender, ExecutedRoutedEventArgs e)
		{
			if (TabControl.SelectedItem == dXTabItemProgram && ConfigReader.GetInstance().GetDeltTVisible() == 1)
			{
				ProgramDeltTTWindow programDeltTTWindow = new ProgramDeltTTWindow();
				programDeltTTWindow.Owner = Utility.GetMainWindow();
				programDeltTTWindow.DeltT = UCPCRProgram.deltT;
				if (programDeltTTWindow.ShowDialog() == true)
				{
					UCPCRProgram.deltT = programDeltTTWindow.DeltT;
					SetModify();
				}
			}
		}

		private void CommandBinding_SapmleType_Executed(object sender, ExecutedRoutedEventArgs e)
		{
			ProjectSampleTypeEditWindow projectSampleTypeEditWindow = new ProjectSampleTypeEditWindow(_ModifyProject);
			projectSampleTypeEditWindow.Owner = Utility.GetMainWindow();
			if (projectSampleTypeEditWindow.ShowDialog() == true)
			{
				_ModifyProject.CustomSampleTypeList.Clear();
				_ModifyProject.CustomSampleTypeList = projectSampleTypeEditWindow.CustomSampleTypeList.ToList();
				SetModify();
			}
		}

		private void TextBoxEx_TextChanged(object sender, TextChangedEventArgs e)
		{
			if (!Utility.ValidateFileName(ModifyProject.BasicInfo.Name))
			{
				MessageBox.Show(string.Format(Utility.GetMessage("msg_filename_illegalcharacter"), Utility.GetFileNameIllegalCharacter().ToString()));
				string text = ModifyProject.BasicInfo.Name;
				StringBuilder fileNameIllegalCharacter = Utility.GetFileNameIllegalCharacter();
				for (int i = 0; i < fileNameIllegalCharacter.Length; i++)
				{
					text = text.Replace(fileNameIllegalCharacter[i].ToString(), string.Empty);
				}
				ModifyProject.BasicInfo.Name = text;
				((TextBox)sender).SelectAll();
			}
		}

		private void TextBoxEx_LostFocus(object sender, RoutedEventArgs e)
		{
			if (((TextBox)sender).Text.Length == 0)
			{
				ModifyProject.BasicInfo.Name = "Project 1";
			}
		}

		[DebuggerNonUserCode]
		[GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
		internal Delegate _CreateDelegate(Type delegateType, string handler)
		{
			return Delegate.CreateDelegate(delegateType, this, handler);
		}
	}
}
